//
//  F.m
//  p2p
//
//  Created by mokai on 14-7-29.
//  Copyright (c) 2014年 cloudyoo. All rights reserved.
//

#import "F.h"

#define FyyyyMMdd @"yyyy-MM-dd"
#define FyyyyMM @"yyyy-MM"
#define FMMdd @"mm-dd"
#define FYMD  @"yyyy年MM月dd日"
#define FD    @"dd"

@implementation F
static NSDateFormatter *formatter ;

+(NSString *)yyyyMMddFromDate:(NSDate *)date{
    [self _init];
    formatter.dateFormat = FyyyyMMdd;
    return [formatter stringFromDate:date];
}
+(NSDate *)yyyyMMddFromString:(NSString *)str{
    [self _init];
    formatter.dateFormat = FyyyyMMdd;
    return [formatter dateFromString:str];
}

+(NSString *)yMdFromDate:(NSDate *)date
{
    [self _init];
    formatter.dateFormat = FYMD;
    return [formatter stringFromDate:date];
 
}

+(NSDate *)yMdFromString:(NSString *)str
{
    [self _init];
    formatter.dateFormat = FYMD;
    return [formatter dateFromString:str];  
}

+(NSString *)yyyyMMFromDate:(NSDate *)date{
    [self _init];
    formatter.dateFormat = FyyyyMM;
    return [formatter stringFromDate:date];
}

+(NSDate *)yyyyMMFromString:(NSString *)str{
    [self _init];
    formatter.dateFormat = FyyyyMM;
    return [formatter dateFromString:str];
}

+(NSString *)MMddFromDate:(NSDate *)date{
    [self _init];
    formatter.dateFormat = FMMdd;
    return [formatter stringFromDate:date];
}

+(NSDate *)MMddFromString:(NSString *)str{
    [self _init];
    formatter.dateFormat = FMMdd;
    return [formatter dateFromString:str];
}

+(NSString *)dFromDate:(NSDate *)date
{
    [self _init];
    formatter.dateFormat = FD;
    return [formatter stringFromDate:date];
}

+(void)_init{
    if(formatter==nil) formatter = [[NSDateFormatter alloc]init];
}


+(NSString *)secureMobile:(NSString *)mobile{
    if (mobile.length==11) {
        return [NSString stringWithFormat:@"%@****%@",[mobile substringToIndex:3],[mobile substringFromIndex:7]];
    }
    return mobile;
}


+(NSString *)secureBankCard:(NSString *)card{
    if (str_isblank(card)) {
        return @"";
    }
    return [NSString stringWithFormat:@"%@****%@",[card substringWithRange:NSMakeRange(0, 4)],[card substringWithRange:NSMakeRange(card.length-4, 4)]];
}

+(NSString *)moneyFormatWithDouble:(double)money{
    return [self moneyFormatWithString:[NSString stringWithFormat:@"%lf",money]];
}

+(NSString *)moneyFormatWithString:(NSString *)money
{
    @try
    {
        if (money.length < 3)
        {
            return money;
        }
        NSString *numStr = money=[F clearZeroWithString:money];
        NSArray *array = [numStr componentsSeparatedByString:@"."];
        NSString *numInt = [array objectAtIndex:0];
        if (numInt.length <= 3)
        {
            return money;
        }
        NSString *suffixStr = @"";
        if ([array count] > 1)
        {
            suffixStr = [NSString stringWithFormat:@".%@",[array objectAtIndex:1]];
        }
        
        NSMutableArray *numArr = [[NSMutableArray alloc] init];
        while (numInt.length > 3)
        {
            NSString *temp = [numInt substringFromIndex:numInt.length - 3];
            numInt = [numInt substringToIndex:numInt.length - 3];
            [numArr addObject:[NSString stringWithFormat:@",%@",temp]];//得到的倒序的数据
        }
        int count = [numArr count];
        for (int i = 0; i < count; i++)
        {
            numInt = [numInt stringByAppendingFormat:@"%@",[numArr objectAtIndex:(count -1 -i)]];
        }
        
        numStr = [NSString stringWithFormat:@"%@%@",numInt,suffixStr];
        return numStr;
    }
    @catch (NSException *exception)
    {
        return money;
    }
    @finally
    {}
}

+(NSString *)clearZeroWithString:(NSString *)stringFloat{
    const char *floatChars = [stringFloat UTF8String];
    NSUInteger length = [stringFloat length];
    NSUInteger zeroLength = 0;
    int i = length-1;
    for(; i>=0; i--)
    {
        if(floatChars[i] == '0'/*0x30*/) {
            zeroLength++;
        } else {
            if(floatChars[i] == '.')
                i--;
            break;
        }
    }
    NSString *returnString;
    if(i == -1) {
        returnString = @"0";
    } else {
        returnString = [stringFloat substringToIndex:i+1];
    }
    return returnString;
}

+(NSString *)clearZeroWithDouble:(double)number{
    return [self clearZeroWithString:[NSString stringWithFormat:@"%lf",number]];
}


@end
